package com.miniapp.controller;

import com.miniapp.entity.Permission;
import com.miniapp.entity.R;
import com.miniapp.service.PermissionService;
import com.miniapp.domain.vo.PermissionQueryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 权限管理控制器
 *
 * @author yourname
 * @date 2025-09-08
 */
@RestController
@RequestMapping("/system/permission")
@Api(tags = "权限管理")
public class PermissionController {

    @Resource
    private PermissionService permissionService;

    /**
     * 分页查询权限列表
     */
    @GetMapping("/page")
    @ApiOperation("分页查询权限列表")
    public R page(PermissionQueryVO queryVO) {
        try {
            List<Permission> permissionList = permissionService.queryPermissionPage(queryVO);
            return R.success(permissionList);
        } catch (Exception e) {
            return R.error("查询权限列表失败: " + e.getMessage());
        }
    }

    /**
     * 查询所有权限
     */
    @GetMapping("/list")
    @ApiOperation("查询所有权限")
    public R list() {
        try {
            List<Permission> permissionList = permissionService.list();
            return R.success(permissionList);
        } catch (Exception e) {
            return R.error("查询权限失败: " + e.getMessage());
        }
    }

    /**
     * 根据ID查询权限
     */
    @GetMapping("/{id}")
    @ApiOperation("根据ID查询权限")
    public R getById(@PathVariable Long id) {
        try {
            Permission permission = permissionService.getById(id);
            return R.success(permission);
        } catch (Exception e) {
            return R.error("查询权限失败: " + e.getMessage());
        }
    }

    /**
     * 根据角色ID查询权限
     */
    @GetMapping("/role/{roleId}")
    @ApiOperation("根据角色ID查询权限")
    public R getByRoleId(@PathVariable Long roleId) {
        try {
            List<String> permissionCodes = permissionService.getPermissionCodesByRoleId(roleId);
            return R.success(permissionCodes);
        } catch (Exception e) {
            return R.error("查询角色权限失败: " + e.getMessage());
        }
    }

    /**
     * 新增权限
     */
    @PostMapping
    @ApiOperation("新增权限")
    public R save(@RequestBody Permission permission) {
        try {
            boolean success = permissionService.savePermission(permission);
            return success ? R.success("新增权限成功") : R.error("新增权限失败");
        } catch (Exception e) {
            return R.error("新增权限失败: " + e.getMessage());
        }
    }

    /**
     * 修改权限
     */
    @PutMapping
    @ApiOperation("修改权限")
    public R update(@RequestBody Permission permission) {
        try {
            boolean success = permissionService.updatePermission(permission);
            return success ? R.success("修改权限成功") : R.error("修改权限失败");
        } catch (Exception e) {
            return R.error("修改权限失败: " + e.getMessage());
        }
    }

    /**
     * 删除权限
     */
    @DeleteMapping("/{id}")
    @ApiOperation("删除权限")
    public R removeById(@PathVariable Long id) {
        try {
            boolean success = permissionService.removeById(id);
            return success ? R.success("删除权限成功") : R.error("删除权限失败");
        } catch (Exception e) {
            return R.error("删除权限失败: " + e.getMessage());
        }
    }

    /**
     * 分配角色权限
     */
    @PostMapping("/assign")
    @ApiOperation("分配角色权限")
    public R assignRolePermissions(@RequestParam Long roleId, @RequestBody List<Long> permissionIds) {
        try {
            boolean success = permissionService.assignRolePermissions(roleId, permissionIds);
            return success ? R.success("分配权限成功") : R.error("分配权限失败");
        } catch (Exception e) {
            return R.error("分配权限失败: " + e.getMessage());
        }
    }
}
